var express = require('express');
var router = express.Router();
var mysql = require('./tool/mysql');

/* GET users listing. */
router.get('/', function(req, res, next) {
  // res.send('respond with a resource');
  res.render('user',{title:"锋影-用户中心",activeIndex:2,isBoss: true});
});
router.get('/casts', function(req, res, next) {
  mysql.connect(function(db){
    console.log("success-casts");
    var querObj = {};
    var showObj = {
      _id: 0,
      id: 1,
      name:1,
      images:1
    };
    var skipNum = 0;
    mysql.findSkip(db,"casts",querObj,showObj,skipNum,function(result){
    // mysql.find(db,"newList",querObj,showObj,function(result){
      // 拿到数据库数据后返回给页面
        obj ={
          title:"锋影-演员列表",
          activeIndex:3,
          isBoss: false,
          result: result
        }
        res.render("casts",obj);
        // console.log(result);
        db.close();
    })
});
  // res.render('casts',{title:"锋影-演员列表",activeIndex:2,isBoss: true});
});

// 后台登录
router.post('/loginAction', function(req, res, next) {
  var obj = req.body;
  console.log(obj)
  mysql.connect(function(db){
      console.log("success----------------------ok")
      var queryObj = obj;
      var showObj = {};
      mysql.find(db, "user", queryObj, showObj, function(result){
          if(result.length > 0) {
              //跳转到首页
              res.cookie("isLogin", 1);
              res.redirect("/")
          }else{
              res.render('login',{title:'精彩家庭-登录',errInfo:"用户名或者密码错误"});
          }
      })
  })
});

// 退出登录
router.get('/logout', function(req, res, next) {
  res.clearCookie('isLogin');
  res.redirect('/')
});

module.exports = router;
